﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FantasyDraft
{
    public partial class Form1 : Form
    {
        Team list1;
        Team list2;
        Team list3;
        Team list4;
        Team playersPool;
        int timeLeft;
        bool playerOkToAdd = true;
        int totalTeams;
        string team1Name;
        string team2Name;
        string team3Name;
        string team4Name;
        Queue<Team> myQueue = new Queue<Team>();
        Stack<Team> myStack = new Stack<Team>();
        Team queueTeam;
        Team stackTeam;
       
        public Form1()
        {
            InitializeComponent();
            StartOver();
        }

        private void RedrawList()
        {
            team1Box.Items.Clear();
            foreach (string playerName in list1.GetPlayerNames())
                team1Box.Items.Add(playerName);
            team1Label.Text = team1Name + " (" + list1.Count + " players)";

            team2Box.Items.Clear();
            foreach (string playerName in list2.GetPlayerNames())
                team2Box.Items.Add(playerName);
            team2Label.Text = team2Name + " (" + list2.Count + " players)";

            team3Box.Visible = false;
            team4Box.Visible = false;
            team3Label.Visible = false;
            team4Label.Visible = false;
            if (totalTeams >= 3)
            {
                team3Box.Visible = true;
                team3Label.Visible = true;
                team3Box.Items.Clear();
                foreach (string playerName in list3.GetPlayerNames())
                    team3Box.Items.Add(playerName);
                team3Label.Text = team3Name + " (" + list3.Count + " players)";
            }

            if (totalTeams == 4)
            {
                team4Box.Visible = true;
                team4Label.Visible = true;
                team4Box.Items.Clear();
                foreach (string playerName in list4.GetPlayerNames())
                    team4Box.Items.Add(playerName);
                team4Label.Text = team4Name + " (" + list4.Count + " players)";
            }
            RedrawPool();
        }

        private void RedrawPool()
        {
            this.moveToTeams.Visible = false;
            playerPoolBox.Items.Clear();
            foreach (string playerName in playersPool.GetPlayerNames())
                playerPoolBox.Items.Add(playerName);
            label2.Text = "AVAILABLE PLAYERS (" + playersPool.Count + " players)";
        }

        private void reset1_Click(object sender, EventArgs e)
        {
            StartOver();
        }

        private void moveToTeams_Click(object sender, EventArgs e)
        {
            DraftPlayer();
        }

        private void DraftPlayer()
        {
            Player playerToMove = playersPool.CheckDuplicate(playerPoolBox.SelectedIndex);
          
            if (myQueue.Count > 0)
            {
                queueTeam = myQueue.Peek();
                foreach (Positions playerPosition in queueTeam.GetPlayerPositions())
                {
                    if (playerToMove.Position == Positions.QB && stackTeam.GetPlayerPositions().Contains(Positions.QB)   )
                    {
                        if (timeLeft > 0)
                            MessageBox.Show("Pick a different Position");
                        playerOkToAdd = false;
                        break;
                    }
                    else
                        playerOkToAdd = true;
                }
                if (playerOkToAdd == true)
                {
                    queueTeam.Add(playersPool.Deal(playerPoolBox.SelectedIndex));
                    queueTeam = myQueue.Dequeue();
                    myStack.Push(queueTeam);
                    if (myQueue.Count > 0)
                        turnLabel.Text = myQueue.Peek().Name;
                    else
                        turnLabel.Text = myStack.Peek().Name;
                    ResetTimer();
                }
            }
            else
            {
                stackTeam = myStack.Peek();
                foreach (Positions playerPosition in stackTeam.GetPlayerPositions())
                {
                    if (playerToMove.Position == Positions.QB && stackTeam.GetPlayerPositions().Contains(Positions.QB)  )
                    { 
                        if (timeLeft > 0)
                            MessageBox.Show("Pick a different Position");
                        playerOkToAdd = false;
                        break;
                    }
                    else
                        playerOkToAdd = true;
                }
                if (playerOkToAdd == true)
                {
                    stackTeam.Add(playersPool.Deal(playerPoolBox.SelectedIndex));
                    stackTeam = myStack.Pop();
                    ResetTimer();
                }
                if (myStack.Count == 0)
                {
                    SetQueue();
                    turnLabel.Text = myQueue.Peek().Name;
                }
                else
                    turnLabel.Text = myStack.Peek().Name;
            }
            RedrawList();
            //End Draft condition
            EndDraftCondition();
        }

        private void EndDraftCondition()
        {
            if (((totalTeams == 4) && (playersPool.Count == 10)) || ((totalTeams == 3) && (playersPool.Count == 15)) || ((totalTeams == 2) && (playersPool.Count == 20)))
            {
                timer1.Stop();
                timerLabel.Text = "";
                turnLabel.Text = "";
                MessageBox.Show("Draft is Complete");
                moveToTeams.Enabled = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random random = new Random();
            if (timeLeft > 0)
            {
                // Display the new time left 
                // by updating the Time Left label.
                timeLeft = timeLeft - 1;
                timerLabel.Text = timeLeft + " seconds";
            }
            else
            {
                // If the user ran out of time, stop the timer, show 
                // a MessageBox, and fill in the answers.
                timer1.Stop();
                timerLabel.Text = "Time's up!";
                MessageBox.Show("Automatic Selection", "Time's up");
                do
                {
                    playerPoolBox.SetSelected(random.Next(playersPool.Count), true);
                    moveToTeams_Click(moveToTeams, EventArgs.Empty);
                } while (playerOkToAdd != true);
            }
        }

        private void ResetTimer()
        {
            // Start the timer.
            timeLeft = 90;
            timerLabel.Text = "90 seconds";
            timer1.Start();
        }

        private void StartOver()
        {
            DisplayStartUps();
            ResetList();
            RedrawList();
            ResetTimer();
            turnLabel.Text = team1Name;
            moveToTeams.Enabled = true;
        }

        private void DisplayStartUps()
        {
            Form2 dialog = new Form2();
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                // Read the contents of testDialog's TextBox.
                totalTeams = dialog.NumOfTeams;
            }
            dialog.Dispose();

            Form3 dialog2 = new Form3(dialog.NumOfTeams);
            if (dialog2.ShowDialog(this) == DialogResult.OK)
            {
                // Read the contents of testDialog's TextBox.
                //label8.Text = dialog.NumOfTeams.ToString();
                team1Name = dialog2.team1;
                team2Name = dialog2.team2;
                team3Name = dialog2.team3;
                team4Name = dialog2.team4;
            }
            dialog2.Dispose();
        }

        private void ResetList()
        {
            myQueue.Clear();
            myStack.Clear();
            list1 = new Team(new Player[] { }, team1Name);
            list2 = new Team(new Player[] { }, team2Name);
            if (totalTeams >= 3)
                list3 = new Team(new Player[] { }, team3Name);
            if (totalTeams == 4)
                list4 = new Team(new Player[] { }, team4Name);
            playersPool = new Team();
            playersPool.Randomize();
            SetQueue();
        }

        private void SetQueue()
        {
            myQueue.Enqueue(list1);
            myQueue.Enqueue(list2);
            if (totalTeams >= 3)
                myQueue.Enqueue(list3);
            if (totalTeams == 4)
            {
                myQueue.Enqueue(list4);
            }
        }

        private void playerPoolBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.moveToTeams.Visible = true;
        }

        private void shuffle2_Click(object sender, EventArgs e)
        {
            playersPool.Randomize();
            RedrawPool();
        }

        private void playerPoolBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DraftPlayer();
        }

        private void team1Box_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
      

         
    }
}